<template>
	<view class="cart page">
		<view v-if="cartItems.length>0">
			
		
		<view class="items" >
			<scroll-view style="height: 100%;" class="scroll" scroll-y="true">
				<view  v-for="(item,index) in cartItems">
						<mall-cart-item @sel="sel" @del="del" :index="index"  @updateNum="updateNum" :item="item" class="item" :key="index"></mall-cart-item>
				</view>
				
			</scroll-view>
		</view>
		
		<mall-cart-bar  @confirm="confirm" :priceInfo="priceInfo" :allSel="allSel" @selAll="selAll"></mall-cart-bar>
		</view>
		<view class="noItem" v-else>
			<text class="info">还没有添加产品~~</text>
			<view class="btn" @click="toMall">去逛逛</view>
			
		</view>
	</view>
</template>

<script>
	import mallCartItem from '../../components/mall-cart-item/mall-cart-item.vue'
	import mallCartBar from  '../../components/mall-cart-bar/mall-cart-bar.vue'
	import {getCart,setCart} from '@/storage/index.js'
	import {changeCart,delCart,selAll,cancelAll,sel} from '@/common/js/cart.js'
	export default {
		components:{
			mallCartItem,mallCartBar
		},
		computed:{
			cartItems(){
				return Object.values(this.cart)
			},
			allSel(){
				let allSel = true
				for (var i = 0; i < this.cartItems.length; i++) {
					if(!this.cartItems[i].sel){
						allSel = false
						break
					}
				}
				return allSel
			},
			priceInfo(){
				let nowPrice = 0
				let oldPrice = 0 
				let number = 0
				this.cartItems.forEach(item => {
					if(item.sel){
						nowPrice += item.nowPrice*item.number
						oldPrice += item.oldPrice*item.number
						number+=item.number
					}
				})
				let cut = (oldPrice - nowPrice).toFixed(2)
				let total = nowPrice.toFixed(2)
				return {cut,total,number}
			}
		},
		methods:{
		toMall(){
			this.$Router.pushTab("/pages/mall/mall")
		},
		confirm(){
			this.$Router.push("/pages/mall/order/commit")
		},
		  updateNum(newVal,index){
			   this.cart =  changeCart(this.cart,this.cartItems[index].key,newVal)
			   setCart(this.cart)
			   // this.$set(this.cartItems,index,)
		  },
		  del(index){
			    this.cart = delCart(this.cart,this.cartItems[index].key)
				setCart(this.cart)
		  },
		  sel(index){
			  this.cart = sel(this.cart,this.cartItems[index].key)
			  setCart(this.cart)
		  },
		  selAll(){
			  if(this.allSel){
				  this.cartItems.forEach(item => {
					  item.sel = false
				  })
				this.cart = cancelAll(this.cart)
				setCart(this.cart)
			  }else{
				  this.cartItems.forEach(item => {
				  		item.sel = true
				  })
				 this.cart = selAll(this.cart)
				 setCart(this.cart)
			  }
		  }
		},
		onShow(){
			 this.cart = getCart()
		},
		data() {
			return {
				cart:{}
			};
		}
	}
</script>

<style lang="stylus" scoped>
	.cart
		background url(../../static/dbg.png) no-repeat
		background-size 100% 150upx
		background-color #EFEFEF
		z-index 2
		display flex
		flex-direction column
		.items
			height 100%
			padding 0 20upx 
			padding-bottom 80upx
			overflow hidden
			display flex
			flex-direction column
			border-radius 10upx
			.item
				border-bottom 1px solid #DCDCDC
				background-color white
		.noItem
			display flex
			align-items center
			justify-content center
			flex-direction column
			height 100%
			.info
				color #999999
				font-size 26upx
			.btn
				display flex
				align-items center
				justify-content center
				background-color  #009C4E
				width 300upx
				color white
				height 68upx
				border-radius 34upx
				margin-top 154upx
</style>
